package org.ohos.rajawali3d.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.ExamplesAbility;
import org.ohos.rajawali3d.examples.data.RajawaBean;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;

public class ExamplesSlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main2);
        if (intent != null) {
            IntentParams extras = intent.getParams();
            if (extras != null) {
                processExtras(extras);
            }
        }
    }

    private void processExtras(IntentParams extras) {
        String url = (String) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_URL);
        String class_name = (String) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_CLASS);
        int type = (int) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_TYPE);
        int name = (int) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_NAME);
        String path = (String) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_PATH);
        Class aCl;
        try {
            aCl = Class.forName(class_name);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        RajawaBean example = new RajawaBean(name, type, aCl, url, path);

        Class aClass = example.getExampleClass();
        try {
            AExampleFragment fragment = (AExampleFragment) aClass.newInstance();
            present(fragment, new Intent());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
